1630A - And Matching - CodeForces Solution


Easy Bit Manipulation

Please click on ads to support us..

Python Code:

import sys
input=sys.stdin.readline
for _ in range(int(input())):
    n,k=map(int,input().split())
    if(n==4 and k==3):
        print(-1)
    else:
        ans=[]
        visted=[False]*n
        if(k!=n-1):
            visted[k]=visted[n-1-k]=visted[0]=visted[n-1]=True
            ans.extend([(k,n-1),(0,n-1-k)])
            if(ans[0]==ans[1]):
                ans.pop()
            for i in range(n):
                if(not visted[i]):
                    visted[i]=visted[n-1-i]=True
                    ans.append((i,n-1-i))
        else:
            visted[0] = visted[n-1] = visted[1] = visted[n - 2] =visted[2] = visted[n-3] = True
            ans.extend([(n-2, n - 1), (1, n -3),(0,2)])
            for i in range(n):
                if (not visted[i]):
                    visted[i] = visted[n - 1 - i] = True
                    ans.append((i, n - 1 - i))

            pass
        for i in ans:
            print(*i)

C++ Code:

#include <iostream>
#include <map>
#include<unordered_map>
#include <iterator>
#include <vector>
#include <string>
#include <algorithm>
#include<utility>
#include<climits>
#include<stack>
#include<set>
#include<cmath>



#define ull unsigned long long
#define ll long long
#define NUM 1000000007
//ull mod  = 998244353;
using namespace std;




void solve()
{
    ll n,k;
    cin>>n>>k;
    ll num = n -1 ;
    if(k ==0)
    {
        for(ll i=0;i<=num/2 ;i++)
        {
            ll ck = i^num;
            cout<<i<<" "<< ck <<endl;
        }
        return ;
    }
    else if(k< n-1)
    {
        ll ck = k^num;
        cout<<0<<" "<<ck<<endl;
        cout<<k<<" "<<num<<endl;
        for(ll i=0;i<=num/2;i++)
        {
            if(i==0 or i==ck or i==k) continue;
            ll ck1 = i^num;
            cout<<i<<" "<<ck1<<endl;
        }
        return;
    }
    if(k == n-1)
    {
        if(n==4ll)
        {
            cout<<-1<<endl;
            return;
        }
        cout<<0<<" "<<2<<endl;
        cout<<1<<" "<<n-3<<endl;
        cout<<n-2<<" "<<n-1<<endl;
        for(ll i=0;i<=num/2;i++)
        {
            if(i<=2) continue;
            ll ck1 = i^num;
            cout<<i<<" "<<ck1<<endl;
        }
    }    
    return;
}


int main()
{
    int t ;
    cin>>t;
   
    while(t--)
    {
        solve();
    }
    return 0;
}


Comments

Submit
0 Comments
More Questions

1324B - Yet Another Palindrome Problem
525A - Vitaliy and Pie
879A - Borya's Diagnosis
1672B - I love AAAB
1673A - Subtle Substring Subtraction
1345A - Puzzle Pieces
711A - Bus to Udayland
779B - Weird Rounding
1703D - Double Strings
1704C - Virus
63A - Sinking Ship
1704B - Luke is a Foodie
298B - Sail
239A - Two Bags of Potatoes
1704E - Count Seconds
682A - Alyona and Numbers
44A - Indian Summer
1133C - Balanced Team
1704A - Two 0-1 Sequences
1467A - Wizard of Orz
1714E - Add Modulo 10
1714A - Everyone Loves to Sleep
764A - Taymyr is calling you
1714B - Remove Prefix
1264F - Beautiful Fibonacci Problem
52A - 123-sequence
1543A - Exciting Bets
1714D - Color with Occurrences
215B - Olympic Medal
1445A - Array Rearrangment